<template>
  <CategoryView :title="typeName+'分类'" :data="data" @modify="modifyCategory" @del="delCategory" @add="addCategory" />
</template>

<script setup>
import { $msg, $box } from "@ui/msg";
import { defineProps, ref } from "vue";
import { TYPE, api } from "@api/category"
import CategoryView from '@/components/CategoryView.vue';

const props = defineProps({
  config: {
    type: Object,
    default: () => {
      return { type: '' }
    }
  }
})
let typeName = '商品'
switch (props.config.type) {
  case 'goods':
    typeName = "商品"
    break
  case 'hotel':
    typeName = "酒店"
    break
  case 'travel':
    typeName = "旅游"
    break
}
const data = ref([
  { id: 'all', name: '全部', code :'all'}
])
getCategory()
function getCategory() {
  api.getCategory(TYPE[props.config.type], val => {
    let list = val.itemList.map((e) => { return { id: e.id, name: e.catName, code: e.catCode } })
    list.unshift({ id: 'all', name: '全部', code :'all'})
    data.value = list
  })
}

function modifyCategory(data) {
  $box.input("修改", '分类名称', data.name, val => {
    api.updateCategory(data.id, val, () => {
      getCategory()
      $msg.succ("修改成功");
    })
  })
}
function delCategory(data) {
  $box.open("删除确认", `是否删除${data.name}`, () => {
    api.delCategory(data.id, () => {
      getCategory()
      $msg.succ("删除成功");
    })
  });
}
function addCategory(data) {
  $box.input("添加", '分类名称', null, val => {
    api.addCategory(val, TYPE[props.config.type], () => {
      getCategory()
      $msg.succ("添加成功");
    })
  })
}


</script>